package com.abner.design.patterns.command;

/**
 * 调用者角色
 * @author abner
 */
public class RemoteController {

	/**
	 * 调用者角色
	 */
	Command[] onCommands;
	Command[] offCommands;

	/**
	 * 撤销命令
	 */
	Command undoCommand;

	/**
	 * 初始化
	 */
	public RemoteController() {
		onCommands = new Command[5];
		offCommands = new Command[5];
		for (int i = 0; i < 5; i++) {
			onCommands[i] = new NoCommand();
			offCommands[i] = new NoCommand();
		}
	}

	/**
	 * 设置命令
	 * @param no 下标
	 * @param onCommand 打开命令
	 * @param offCommand 关闭命令
	 */
	public void setCommand(int no, Command onCommand, Command offCommand) {
		onCommands[no] = onCommand;
		offCommands[no] = offCommand;
	}

	/**
	 * 按下开按钮
	 * @param no
	 */
	public void onButtonWasPushed(int no) {
		onCommands[no].execute();
		undoCommand = onCommands[no];
	}

	/**
	 * 按下关按钮
	 * @param no
	 */
	public void offButtonWasPushed(int no) {
		offCommands[no].execute();
		undoCommand = offCommands[no];
	}

	/**
	 * 撤销
	 */
	public void undoButtonWasPushed() {
		undoCommand.undo();
	}
}
